﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.BLL.Center;
using SHKF.B2C.Hosted;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 

namespace SHKF.B2C.Mannage.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class CompanyController : BaseController
    {
        private readonly CompanyBLL companyBLL;

        public CompanyController(CompanyBLL companyBLL,
           ILogger<CompanyController> logger) : base(logger)
        {
            this.companyBLL = companyBLL; 
        }

        [HttpPost("Save")]
        public CompanyDto Save(CompanyEntity company)
        {
            CompanyDto dto = new CompanyDto();
            try
            {
                dto.data = this.companyBLL.Save(company); 
            }
            catch (Exception ex)
            {
                dto = new CompanyDto();
                dto.SetException(ex);
                base.WriteException("Company>>Save", ex);
            }

            return dto;
        }

        [HttpGet("Delete")]
        public DataDto Delete(int SN)
        {
            DataDto dto = new DataDto();
            try
            {
                int iResult = this.companyBLL.Delete(SN);
                if (iResult == 0)
                {
                    dto.error = new Error()
                    {
                        code = -1,
                        msg = "成功行数为0"
                    };
                }
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("Company>>Delete", ex);
            }

            return dto;
        }
 
        [HttpGet("List")]
        public CompanyListDto List(string CompanyName)
        {
            CompanyListDto dto = new CompanyListDto();
            try
            {
                dto.data = this.companyBLL.Query(CompanyName);
            }
            catch (Exception ex)
            {
                dto = new CompanyListDto();
                dto.SetException(ex);
                base.WriteException("Company>>List", ex);
            }

            return dto;
        }
    }
}
